﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Migrator.Util;
using Migrator.Entities;

namespace Migrator.Services.MobileIPNodeService
{
    /// <summary>
    /// Mobile node pending registrations table
    /// </summary>
    public class RegistrationsTable
    {
        /// <summary>
        /// Adds a new entry to the pending registrations table
        /// </summary>
        /// <param name="entry">The entry to add</param>
        public void Add(Entry entry)
        {
            table[entry.DestinationIP] = entry;
        }

        public bool Contains(IPAddress destinationIP)
        {
            return this.table.ContainsKey(destinationIP);
        }

        public int Count
        {
            get { return this.table.Count; }
        }

        /// <summary>
        /// Removes the entry for the given destination IP.
        /// </summary>
        /// <param name="destinationIP"The destination IP of the entry to remove></param>
        public void Remove(IPAddress destinationIP)
        {
            this.table.Remove(destinationIP);
        }

        /// <summary>
        /// Removes expired entries
        /// </summary>
        /// <param name="now">The current clock time</param>
        public void RemoveExpired(int now)
        {
            var toRemove = this.table.Values.Where(entry => entry.ExpirationTime < now).ToList();
            foreach (var entry in toRemove)
            {
                this.table.Remove(entry.DestinationIP);
            }

        }

        public Entry this[IPAddress destinationIP]
        {
            get { return table[destinationIP]; }
        }

        private Dictionary<IPAddress, Entry> table = new Dictionary<IPAddress, Entry>();

        /// <summary>
        /// Mobile node pending registrations table entry
        /// </summary>
        public class Entry
        {
            public Entry(Node foreignAgent, IPAddress destinationIP, IPAddress careOfAddress,
                int expirationTime, ushort requestedLifetime)  
            {
                this.ForeignAgent = foreignAgent;
                this.DestinationIP = destinationIP;
                this.CareOfAddress = careOfAddress;
                this.ExpirationTime = expirationTime;
                this.RequestedLifetime = requestedLifetime;
            }

            public Node ForeignAgent
            {
                get;
                private set;
            }

            public IPAddress DestinationIP
            {
                get;
                private set;
            }

            public IPAddress CareOfAddress
            {
                get;
                private set;
            }

            public int ExpirationTime
            {
                get;
                private set;
            }

            public ushort RequestedLifetime
            {
                get;
                private set;
            }
        }
    }
}
